﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LogUi.cs" company="" />
// <summary>
//   A logger to both the 'Trace' utility and to present a message-box on the user's UI.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace FormsUtilities
{
    using System;
    using System.Diagnostics;
    using System.Globalization;
    using System.Windows.Forms;

    /// <summary>
    /// A logger to both the 'Trace' utility and to present a message-box on the user's UI.
    /// </summary>
    public static class LogUi
    {
        /// <summary>
        /// Log an information message.
        /// </summary>
        /// <param name="messageFormat">The message format</param>
        /// <param name="args">Additional arguments if needed.</param>
        public static void LogInfo(string messageFormat, params object[] args)
        {
            string message = string.Format(CultureInfo.InvariantCulture, messageFormat, args);
            string extendedMessage = string.Format("{0}{1}{1}(Hit Ctrl+C to copy this message contents)", message, Environment.NewLine);

            Trace.TraceInformation(message);
            MessageBox.Show(extendedMessage, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// Log a warning message.
        /// </summary>
        /// <param name="messageFormat">The message format</param>
        /// <param name="args">Additional arguments if needed.</param>
        public static void LogWarning(string messageFormat, params object[] args)
        {
            string message = string.Format(CultureInfo.InvariantCulture, messageFormat, args);

            Trace.TraceWarning(message);
            MessageBox.Show(message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        /// <summary>
        /// Log an error message.
        /// </summary>
        /// <param name="messageFormat">The message format</param>
        /// <param name="args">Additional arguments if needed.</param>
        public static void LogError(string messageFormat, params object[] args)
        {
            string message = string.Format(CultureInfo.InvariantCulture, messageFormat, args);

            Trace.TraceError(message);
            MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// Log a critical error message.
        /// </summary>
        /// <param name="messageFormat">The message format</param>
        /// <param name="args">Additional arguments if needed.</param>
        public static void LogFatal(string messageFormat, params object[] args)
        {
            string message = string.Format(CultureInfo.InvariantCulture, "FATAL: " + messageFormat, args);

            Trace.TraceError(message);
            MessageBox.Show(message, "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// Logs a question and returns the user's answer (yes/no, ok/cancel).
        /// </summary>
        /// <param name="answerButtonSet">The answer button set to display on the message box.</param>
        /// <param name="messageFormat">The format of the message string.</param>
        /// <param name="args">The arguments to the message string.</param>
        /// <returns>The user choice for an answer.</returns>
        public static DialogResult LogQuestion(MessageBoxButtons answerButtonSet, string messageFormat, params object[] args)
        {
            string message = string.Format(CultureInfo.InvariantCulture, messageFormat, args);
            var answer = MessageBox.Show(message, "Question", answerButtonSet, MessageBoxIcon.Question);

            Trace.TraceInformation("Question '{0}' answered by '{1}'.", message, answer);

            return answer;
        }
    }
}
